package cn.touna.jss.modules.custom.batch;

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

public class RequestProcessor implements ItemProcessor {

    private RestTemplate restTemplate = null;
    private HttpHeaders headers;
    private String url;

    public RequestProcessor(String url){
        this.url = url;
        headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
    }

    @Override
    public JSONObject process(JSONObject item) {
        restTemplate = new RestTemplate();
        String request = item.getString(BatchConstant.COLUMN_REQUEST);
        HttpEntity<String> requestEntity = new HttpEntity<>(request, headers);
        try{
            String res = restTemplate.postForObject( url,requestEntity , String.class );
            return JSONObject.parseObject(res);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
